package com.lj.back.manage.scaffold.common.exception;

import com.lj.back.manage.scaffold.common.constant.HttpConstant;
import com.lj.back.manage.scaffold.common.enums.ResponseEnum;
import com.lj.back.manage.scaffold.common.response.R;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/error")
public class ErrorController {

    /**
     * 未授权异常
     * @param request 请求对象
     * @return 错误对象
     */
    @RequestMapping("/unauthorized")
    public R<Object> unauthorized(HttpServletRequest request){
        Object errorMessage = request.getAttribute(HttpConstant.REQUEST_ATTRIBUTE_ERROR_MESSAGE_KEY);
        String message = "token已失效,请重新登录";
        if (errorMessage != null){
            message = errorMessage.toString();
        }
        return R.build(ResponseEnum.UNAUTHORIZED).message(message);
    }
}
